<template>
  <el-form ref="formIns" :model="user" :rules="rules" label-width="100px">
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="user.userName" placeholder="请输入用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pwd">
      <el-input v-model="user.pwd" type="password" placeholder="请输入密码"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="danger" @click="formIns.resetFields()">重置</el-button>
      <el-button type="primary" @click="handleSubmit">登录</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import useUserStore from "@s/user";
import { ElMessage } from "element-plus";
const router = useRouter();
const route = useRoute();
const userStore = useUserStore();
const formIns = ref<any>(null);
const rules = {
  userName: [
    {
      pattern: /^[a-z]\w{3,7}$/,
      message: "4到8位字母数字_组成字母开头",
      trigger: "blur",
    },
  ],
  pwd: [
    {
      pattern: /^[a-z]\w{3,7}$/,
      message: "4到8位字母数字_组成字母开头",
      trigger: "blur",
    },
  ],
};
const user = reactive({
  userName: "",
  pwd: "",
});

const handleSubmit = () => {
  formIns.value
    .validate()
    .then(() => {
      userStore.userLogin(user, { router, route });
    })
    .catch((err: any) => {
      ElMessage.error("校验失败");
    });
};
defineOptions({
  name: "LoginPage",
});
</script>

<style scoped></style>
